'use strict';

const conn = require('../../../libs/Conn');
const _ = require('underscore');

module.exports = async function (params) {
    let sql;
    let rec;
    switch (params.progress_type) {
        case '23' :
            sql = `SELECT
                            foundation_test_tanks,
                            material_check_a,
                            foundation_test,
                            important_part_acceptance,
                            first_conversion_reinforcement,
                            material_check_b,
                            standard_reinforcement,
                            top_reinforcement,
                            quality_sampling,
                            concrete_structure,
                            material_check_c,
                            main_structure,
                            other_projects
                        FROM
                            engineering_progress_file
                        WHERE
	                        engineering_id = '${params.engineering_id}';SELECT party_a_material,construction_material,acceptance_data FROM engineering_progress_completion WHERE engineering_id = '${params.engineering_id}'`;
            rec = await conn(sql);
            for(let data in rec[0][0]) {
                if (rec[0][0][data] !== 3) {
                    return {success: false, message: '请先完成前面流程'}
                }
            }
                return {success:true,data:rec[1][0]};
        case '24' :
            sql = `SELECT
                            completion_initial_inspection
                        FROM
                            engineering_progress_file
                        WHERE
	                        engineering_id = '${params.engineering_id}';
	                        SELECT
                                uf.user_name,
                                uf.user_id
                            FROM
                                progress_information_user_middle pium
                            LEFT JOIN user_file uf ON uf.user_id = pium.user_id
                            WHERE
                                pium.information_id IN (
                                    SELECT
                                        information_id
                                    FROM
                                        engineering_progress_completion
                                    WHERE
                                        engineering_id = '${params.engineering_id}'
                                );SELECT epc.remarks,DATE_FORMAT(
                           epc.completion_date,
                            '%Y-%m-%d'
                        ) AS completion_date,
                        DATE_FORMAT(
                           epc.review_pass_date,
                            '%Y-%m-%d'
                        ) AS review_pass_date,
                        DATE_FORMAT(
                           epc.inspection_date,
                            '%Y-%m-%d'
                        ) AS inspection_date,epc.user_id,uf.user_name FROM engineering_progress_completion epc LEFT JOIN user_file uf ON uf.user_id = epc.user_id WHERE epc.engineering_id = '${params.engineering_id}'`;
            rec = await conn(sql);
            if(rec[0][0].completion_initial_inspection !== 3){
                    return {success: false,message:'请先完成竣工初验'}
            }else{
                let userIds = [];
                let userName = ``;
                _.each(rec[1],function (data) {
                    userIds.push(data.user_id)
                    userName +=data.user_name + ','
                });
                if(userName.length>0){
                    userName = userName.slice(0,userName.length-1);
                }
                rec[2][0].user_ids = userIds;
                rec[2][0].user_names = userName;

                return {success:true,data:rec[2][0]};
            }
        case '25' :
            sql = `SELECT
                            completion_acceptance
                        FROM
                            engineering_progress_file
                        WHERE
	                        engineering_id = '${params.engineering_id}';SELECT epf.remarks,DATE_FORMAT(
                           epf.filing_date,
                            '%Y-%m-%d'
                        ) AS filing_date,DATE_FORMAT(
                           epf.inspection_date,
                            '%Y-%m-%d'
                        ) AS inspection_date,
                        epf.user_id,uf.user_name FROM engineering_progress_filing epf LEFT JOIN user_file uf ON uf.user_id = epf.user_id WHERE engineering_id = '${params.engineering_id}'`;
            rec = await conn(sql);
            if(rec[0][0].completion_acceptance !== 2){
                return {success: false,message:'请先完成竣工验收'}
            }else{
                return {success:true,data:rec[1][0]};
            }
    }

};